﻿#include "MountDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../MirSounds/SoundManager.h"
#include "../../../Shared/Enums.h"
#include "../GameScene.h"
#include "../../../Shared/Data/ItemData.h"
#include "../../MirObjects/MapObject.h"
#include "../../Forms/Client.CMain.h"
#include "../../../Shared/ClientPackets.h"
#include "../../MirNetwork/Network.h"
#include "../../MirControls/MirMessageBox.h"
#include "../../../Shared/Language.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirObjects;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

    MountDialog::MountDialog()
    {
        setIndex(167);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        setSort(true);
        Point tempVar(10, 30);
        setLocation(&tempVar);
        BeforeDraw += MountDialog_BeforeDraw;

        MountName = new MirLabel();
        Point tempVar2(30, 10);
        MountName->setLocation(&tempVar2);
        MountName->setDrawFormat(TextFormatFlags::VerticalCenter | TextFormatFlags::HorizontalCenter);
        MountName->setParent(this);
        MountName->setNotControl(true);
        MountLoyalty = new MirLabel();
        Point tempVar3(30, 30);
        MountLoyalty->setLocation(&tempVar3);
        MountLoyalty->setDrawFormat(TextFormatFlags::VerticalCenter | TextFormatFlags::HorizontalCenter);
        MountLoyalty->setParent(this);
        MountLoyalty->setNotControl(true);

        MountButton = new MirButton();
        MountButton->setLibrary(Libraries::Prguse);
        MountButton->setParent(this);
        MountButton->setSound(SoundList::ButtonA);
        Point tempVar4(262, 70);
        MountButton->setLocation(&tempVar4);
        MountButton->Click += [&] (o, e)
        {
            if (CanRide())
            {
                Ride();
            }
        };

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };

        HelpButton = new MirButton();
        HelpButton->setIndex(257);
        HelpButton->setHoverIndex(258);
        HelpButton->setPressedIndex(259);
        HelpButton->setLibrary(Libraries::Prguse2);
        HelpButton->setParent(this);
        HelpButton->setSound(SoundList::ButtonA);
        HelpButton->Click += [&] (o, e)
        {
            GameScene::Scene->HelpDialog->DisplayPage("Mounts");
        };

        MountImage = new MirAnimatedControl();
        MountImage->setAnimated(false);
        MountImage->setAnimationCount(16);
        MountImage->setAnimationDelay(100);
        MountImage->setIndex(0);
        MountImage->setLibrary(Libraries::Prguse);
        MountImage->setLoop(true);
        MountImage->setParent(this);
        MountImage->setNotControl(true);
        MountImage->setUseOffSet(true);

//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to the C# 'typeof' operator:
        Grid = std::vector<MirItemCell*>(Enum::GetNames(typeof(MountSlot)).size());

        MirItemCell *tempVar5 = new MirItemCell();
        tempVar5->setItemSlot(static_cast<int>(MountSlot::Reins));
        tempVar5->setGridType(MirGridType::Mount);
        tempVar5->setParent(this);
        Size tempVar6(34, 30);
        tempVar5->setSize(&tempVar6);
        Grid[static_cast<int>(MountSlot::Reins)] = tempVar5;
        MirItemCell *tempVar7 = new MirItemCell();
        tempVar7->setItemSlot(static_cast<int>(MountSlot::Bells));
        tempVar7->setGridType(MirGridType::Mount);
        tempVar7->setParent(this);
        Size tempVar8(34, 30);
        tempVar7->setSize(&tempVar8);
        Grid[static_cast<int>(MountSlot::Bells)] = tempVar7;

        MirItemCell *tempVar9 = new MirItemCell();
        tempVar9->setItemSlot(static_cast<int>(MountSlot::Saddle));
        tempVar9->setGridType(MirGridType::Mount);
        tempVar9->setParent(this);
        Size tempVar10(34, 30);
        tempVar9->setSize(&tempVar10);
        Grid[static_cast<int>(MountSlot::Saddle)] = tempVar9;

        MirItemCell *tempVar11 = new MirItemCell();
        tempVar11->setItemSlot(static_cast<int>(MountSlot::Ribbon));
        tempVar11->setGridType(MirGridType::Mount);
        tempVar11->setParent(this);
        Size tempVar12(34, 30);
        tempVar11->setSize(&tempVar12);
        Grid[static_cast<int>(MountSlot::Ribbon)] = tempVar11;


        MirItemCell *tempVar13 = new MirItemCell();
        tempVar13->setItemSlot(static_cast<int>(MountSlot::Mask));
        tempVar13->setGridType(MirGridType::Mount);
        tempVar13->setParent(this);
        Size tempVar14(34, 30);
        tempVar13->setSize(&tempVar14);
        Grid[static_cast<int>(MountSlot::Mask)] = tempVar13;

        delete tempVar13;
        delete tempVar11;
        delete tempVar9;
        delete tempVar7;
        delete tempVar5;
    }

    void MountDialog::MountDialog_BeforeDraw(std::any sender, EventArgs *e)
    {
        RefreshDialog();
    }

    void MountDialog::RefreshDialog()
    {
        SwitchType();
        DrawMountAnimation();
    }

    void MountDialog::SwitchType()
    {
        UserItem *MountItem = GameScene::getUser()->Equipment[static_cast<int>(EquipmentSlot::Mount)];
        std::vector<UserItem*> MountSlots;

        if (MountItem != nullptr)
        {
            MountSlots = MountItem->Slots;
        }

        if (MountSlots.empty())
        {
            return;
        }

        int x = 0, y = 0;

        switch (MountSlots.size())
        {
            case 4:
                setIndex(160);
                StartIndex = 1170;
                Size tempVar(208, 15);
                MountName->setSize(&tempVar);
                Size tempVar2(208, 15);
                MountLoyalty->setSize(&tempVar2);
                Point tempVar3(110, 250);
                MountImage->setLocation(&tempVar3);
                MountButton->setIndex(164);
                MountButton->setHoverIndex(165);
                MountButton->setPressedIndex(166);
                Point tempVar4(210, 70);
                MountButton->setLocation(&tempVar4);
                Point tempVar5(245, 3);
                CloseButton->setLocation(&tempVar5);
                Point tempVar6(221, 3);
                HelpButton->setLocation(&tempVar6);
                Grid[static_cast<int>(MountSlot::Mask)]->setVisible(false);
                x = 1;
                y = 1;
                break;
            case 5:
                setIndex(167);
                StartIndex = 1330;
                Size tempVar7(260, 15);
                MountName->setSize(&tempVar7);
                Size tempVar8(260, 15);
                MountLoyalty->setSize(&tempVar8);
                Point tempVar9(0, 70);
                MountImage->setLocation(&tempVar9);
                MountButton->setIndex(155);
                MountButton->setHoverIndex(156);
                MountButton->setPressedIndex(157);
                Point tempVar10(262, 70);
                MountButton->setLocation(&tempVar10);
                Point tempVar11(297, 3);
                CloseButton->setLocation(&tempVar11);
                Point tempVar12(274, 3);
                HelpButton->setLocation(&tempVar12);
                Grid[static_cast<int>(MountSlot::Mask)]->setVisible(true);
                x = 0;
                y = 0;
                break;
        }

        Point tempVar13(36 + x, 323 + y);
        Grid[static_cast<int>(MountSlot::Reins)]->setLocation(&tempVar13);
        Point tempVar14(90 + x, 323 + y);
        Grid[static_cast<int>(MountSlot::Bells)]->setLocation(&tempVar14);
        Point tempVar15(144 + x, 323 + y);
        Grid[static_cast<int>(MountSlot::Saddle)]->setLocation(&tempVar15);
        Point tempVar16(198 + x, 323 + y);
        Grid[static_cast<int>(MountSlot::Ribbon)]->setLocation(&tempVar16);
        Point tempVar17(252 + x, 323 + y);
        Grid[static_cast<int>(MountSlot::Mask)]->setLocation(&tempVar17);
    }

    void MountDialog::DrawMountAnimation()
    {
        if (GameScene::getUser()->MountType < 0)
        {
            MountImage->setIndex(0);
            MountImage->setAnimated(false);
        }
        else
        {
            MountImage->setIndex(StartIndex + (GameScene::getUser()->MountType * 20));
            MountImage->setAnimated(true);

            UserItem *item = MapObject::User->Equipment[static_cast<int>(EquipmentSlot::Mount)];

            if (item != nullptr)
            {
                MountName->setText(item->getFriendlyName());
                MountLoyalty->setText(StringHelper::formatSimple("{0} / {1} Loyalty", item->CurrentDura, item->MaxDura));
            }
        }

    }

    bool MountDialog::CanRide()
    {
        if (GameScene::getUser()->MountType < 0 || GameScene::getUser()->MountTime + 500 > CMain::Time)
        {
            return false;
        }
        if (GameScene::getUser()->CurrentAction != MirAction::Standing && GameScene::getUser()->CurrentAction != MirAction::MountStanding)
        {
            return false;
        }

        return true;
    }

    void MountDialog::Ride()
    {
        C::Chat *tempVar = new C::Chat();
        tempVar->Message = "@ride";
        Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void MountDialog::Show()
    {
        if (getVisible())
        {
            return;
        }
        if (GameScene::getUser()->MountType < 0)
        {
            MirMessageBox *messageBox = new MirMessageBox(GameLanguage::NoMount, MirMessageBoxButtons::OK);
            messageBox->Show();

            delete messageBox;
            return;
        }

        setVisible(true);
    }

    MirItemCell *MountDialog::GetCell(unsigned long long id)
    {
        for (int i = 0; i < Grid.size(); i++)
        {
            if (Grid[i]->getItem() == nullptr || Grid[i]->getItem().UniqueID != id)
            {
                continue;
            }
            return Grid[i];
        }
        return nullptr;
    }
}
